package com.coocaa.x.app.hall.parser;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.View;

import com.coocaa.x.app.hall.view.HallItemView;
import com.coocaa.x.framework.utils.LogUtils;
import com.coocaa.x.uipackage.BaseView;

/**
 * Created by sunny on 15-6-16.
 */
public class ViewParser implements IParseInterface<BaseView> {
    HallItemView itemView;
    Context mContext;
    ViewParser viewParser = null;
    IViewLoadOverListener iViewLoadOverListener = null;

    public interface IViewLoadOverListener {
        public void viewImageLoadOver(View view, Bitmap bitmap);
    }

    public ViewParser(Context context) {
        mContext = context;
        viewParser = this;
    }

    @Override
    public BaseView parse(String json) {
        itemView = new HallItemView(mContext);
        itemView.setIViewLoadOverListener(iViewLoadOverListener);
        try {
            ParserData.View_Node view = ParserData.View_Node.parseJObject(json, ParserData.View_Node.class);
            LogUtils.i("", "sunny add parse:" + view.bg + ";   " + view.title + ";   " + view.tip);
            parseBg(view);
            parseTitle(view);
            parseTip(view, false);
        } catch (Exception e) {
            LogUtils.i("", "sunny parse Exception:" + e.toString());
        }

        return itemView;
    }

    public BaseView parse(String json, boolean isBigTip) {
        itemView = new HallItemView(mContext);
        itemView.setIViewLoadOverListener(iViewLoadOverListener);
        try {
            ParserData.View_Node view = ParserData.View_Node.parseJObject(json, ParserData.View_Node.class);
            LogUtils.i("", "sunny add parse:" + view.bg + ";   " + view.title + ";   " + view.tip);
            parseBg(view);
            parseTitle(view);
            parseTip(view, isBigTip);
        } catch (Exception e) {
            LogUtils.i("", "sunny parse Exception:" + e.toString());
        }

        return itemView;
    }


    private void parseBg(ParserData.View_Node view) {
        if (view.bg == null)
            return;

        ParserData.View_Node.View_bg_Node bg = ParserData.View_Node.View_bg_Node.parseJObject(view.bg, ParserData.View_Node.View_bg_Node.class);
        if (bg.bg_standard != null) {
            ParserData.View_Node.View_bg_Node.BG_Standard bg_standard = ParserData.View_Node.View_bg_Node.BG_Standard.parseJObject(bg.bg_standard, ParserData.View_Node.View_bg_Node.BG_Standard.class);
            if (bg_standard != null)
                itemView.addBgLayout(bg_standard.image_url);
        } else {
            ParserData.View_Node.View_bg_Node.BG_Gallery bg_gallery = ParserData.View_Node.View_bg_Node.BG_Gallery.parseJObject(bg.bg_gallery, ParserData.View_Node.View_bg_Node.BG_Gallery.class);
            if (bg_gallery != null)
                itemView.addBgGalleryLayout(bg_gallery.image_list);
        }
    }

    private void parseTitle(ParserData.View_Node view) {
        if (view.title != null) {
            ParserData.View_Node.View_title_Node titleNode = ParserData.View_Node.View_title_Node.parseJObject(view.title, ParserData.View_Node.View_title_Node.class);
            if (titleNode != null) {
                ParserData.View_Node.View_title_Node.Title_Standard title_standard = ParserData.View_Node.View_title_Node.Title_Standard.parseJObject(titleNode.title_standard,
                        ParserData.View_Node.View_title_Node.Title_Standard.class);
                if (title_standard != null)
                    itemView.addTitleLayout(title_standard.title);
            }
        }

    }

    private void parseTip(ParserData.View_Node view, boolean isBigTip) {
        if (view.tip != null) {
            ParserData.View_Node.View_tip_Node tipNode = ParserData.View_Node.View_title_Node.parseJObject(view.tip, ParserData.View_Node.View_tip_Node.class);
            if (tipNode != null) {
                ParserData.View_Node.View_tip_Node.Tip_Standard tip_standard = ParserData.View_Node.View_tip_Node.Tip_Standard.parseJObject(tipNode.tip_standard,
                        ParserData.View_Node.View_tip_Node.Tip_Standard.class);
                if (tip_standard != null)
                    itemView.addTipLayout(tip_standard.tip_url, isBigTip);
            }
        }

    }

    public void setIViewLoadOverListener(IViewLoadOverListener listener) {
        this.iViewLoadOverListener = listener;
    }

}
